var clientVersion={
    init:function(){
        var self=this;
        self.shadeDiv = $("#shade-div");
        self.loadTree();
        self.bind()
    },
    bind:function(){
        var self=this;
        $(".memberList li p.one span").each(function(){
            if($(this).parent("p").siblings("ul").length>0){
                $(this).addClass("cur");
                $(this).live("click",function(){
                    if($(this).parent("p").siblings("ul").is(":hidden")){
                        $(this).parent("p").siblings("ul").slideDown()
                    }else{
                        $(this).parent("p").siblings("ul").slideUp()
                    }
                })
            }
        });

        $(".memberList li p.three input").each(function(){
            var cName=$(this).parents("ul.memberList").find("h1").find("input").attr("id");
            $(this).addClass(cName)
        });
        $(".memberList li.step1>p.three input").each(function(){
            var cName=$(this).attr("class");
            if($(this).parent("p").siblings("ul").length>0){
                $(this).attr("id",cName+"1");
                $(this).parent("p").siblings("ul").find("li.step2 p.three input").addClass($(this).attr("id"))
            }

        })
        $(".memberList li.step2>p.three input").each(function(){
            var cNames=$(this).attr("class").split(" ");
            var cName=cNames[1]
            if($(this).parent("p").siblings("ul").length>0){
                $(this).attr("id",cName+"1");
                $(this).parent("p").siblings("ul").find("li.step3 p.three input").addClass($(this).attr("id"))
            }
        })
        self.bindEvents();

    },
    bindEvents:function(){
        var self=this;
//        全选操作
        $.checkAll("inputItem1");
        $.checkAll("inputItem2");
        $.checkAll("checkBox1");
        $.checkAll("checkBox11");
        $.checkAll("checkBox111");
        $.checkAll("checkBox1111");
        $("#addClient").click(function(){
            self.shadeDiv.show();
            $('#addVersion').setPosition().show();
        })
        $("#searchDep").textClean();
        //        搜索
        $("#searchName input").textClean()
        $("#searchName input").focus(function(){
            $(this).siblings("span").show();
        })
        $("#searchName input").blur(function(){
            if($.trim($(this).val()) == ""){
                $(this).siblings("span").hide();
            };

        })
        $(".removeSearch").click(function(){
            $(this).siblings("input").val("")
        })
        $('.searchName').keydown(function(e){
            if(e.keyCode==13){
                $(this).click(); //处理事件
            }
        });
        $(".dan").click(function(){
            if($(this).attr("checked")=="checked"){
                $("div.bt").show();
            }else{
                $("div.bt").hide();
            }
        })
        $(".dans").click(function(){
            if($(this).attr("checked")=="checked"){
                $("div.bt").hide();
            }
        })
        //发布版本
        $("#fbButton").click(function(){
        	var verNum=$("#verNum").val();
        	if(verNum==null||verNum==""){
        		alert("升级版本号不能为空！");
        		return false;
        	}
        	/*
        	var fullUrl=$("#fullUrl").val();
        	if(fullUrl==null||fullUrl==""){
        		alert("下载路径不能为空！");
        		return false;
        	}*/
        	
        	
        	var deptChecked=$.fn.zTree.getZTreeObj("treeDemo1").getCheckedNodes();
        	var deptIds="";//部门菜单选中
        	for(var i=0;i<deptChecked.length;i++){
        		if(i==0){
        			deptIds+=deptChecked[i].id;
        		}else{
        			deptIds+=","+deptChecked[i].id;
        		}
        	}
     
        	$("#editForm_deptIds").val(deptIds);
        	$.post(ctx+"/upgrade/client_version!save.xhtml",$("#saveForm").serialize(),function(data){
        		//common.refreshPage();
        		alert(data);
        	});
        })
        
        $("#updateFlag").change(function(){
            if($(this).val()=="2"){
               $("span.remid").show();
            }else{
                $("span.remid").hide();
            }
        });
//        全量下载路径

        $(".downloads").live("click",function(){
             $(".downloadPath").show();
            var name=$(this).val()
            var i=$(this).index()-1;

            self.addDownloadPath(name,i);

        })
        $(".downloadPath button").live("click",function(){
            var l=$(".downloadPath li").length;
            if(l==1){
                $(this).parent("li").remove();
                $(".downloadPath").hide();
            }else{
                $(this).parent("li").remove();
            }
        })

    },
    addDownloadPath:function(val,i){
        var l=$(".downloadPath li").length;
        if(l<10){
            var path="";
            var qlUrl= $("input[name='qlUrl']");
            var zlUrl= $("input[name='zlUrl']");
         
           // alert(zlUrl.length);
            if(val=='全量下载路径'){
              if(qlUrl.length<=4){
             path="<li class='clearfix'><label>"+val+"：</label><input type='text' class='itext' name='qlUrl'  value=''><button class='btn gray-btn'>删除</button> </li>";
             $(".downloadPath ul").eq(i).append(path);
              }
            }else{
            	if(zlUrl.length<=4){
             path="<li class='clearfix'><label>"+val+"：</label><input type='text' class='itext' name='zlUrl' value=''><button class='btn gray-btn'>删除</button> </li>"; 	
             $(".downloadPath ul").eq(i).append(path);
            	}
            }
           
        }

    },
    loadTree:function(){
       
    	
    	//加载部门数据
    	clientVersion.ajaxPOST(ctx+"/upgrade/client_version!getDeptTree.xhtml","",function(data){
    		$.fn.zTree.init($("#treeDemo1"), setting1, data);
    		
    		var ver_num=$("#ver_num").val();
    		var clientType=$("#clientType").val();
    		var treeDemo3=$.fn.zTree.getZTreeObj("treeDemo1");
    		treeDemo3.setting.check.chkboxType = { "Y" : "", "N" : "" };
    		
    		//选中部门
    		clientVersion.ajaxPOST(ctx+"/upgrade/client_version!getDeptTreeIds.xhtml",{ver_num:ver_num,clientType:clientType},function(data){
        	
        		for(var i=0;i<data.length;i++){
        			
        			if(data[i].id!=0){
        				
            		var node = treeDemo3.getNodeByParam("id",data[i].id);
            		//alert(node.isParent);
            		//if(!node.isParent){
            		treeDemo3.checkNode(node, true, true);
            		//}
        			}
            	}
        	});
    		
    	});
    	
    	//$.fn.zTree.init($("#treeDemo"), setting1, zNode1);
      //  $.fn.zTree.init($("#treeDemo1"), setting1, zNode1);
    },
    ajaxGET:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"GET", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    },
    ajaxPOST:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"POST", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    }
}
$(function(){
    clientVersion.init()
})